/*
* Copyright (C) 2012-2014 qar
* License: http://www.gnu.org/licenses/gpl.html GPL version 2
*/

#ifndef CIPHER_H
#define CIPHER_H

#include <QtCore>

class Encryptor;

class Cipher
{
public:
    enum EncryptionAlgorithm {
		NoEncryption = 0,
		Aes256 = 1,
		Salsa20 = 2
    };

    Cipher(EncryptionAlgorithm algorithm, QString keyFile);
    virtual ~Cipher();

	void setAlgorithm(EncryptionAlgorithm algorithm, QString keyFile, bool canGenerate);
    EncryptionAlgorithm getAlgorithm() const;
    QString getAlgorithmName() const;

	QByteArray getKey() const;
	static bool saveKey(QByteArray key, QString keyFile);
	QByteArray generateKey(int length) const;
	static QByteArray generateKey(QByteArray password, int length, QString keyFile);

    qint64 blockSize() const;

    // The length of the data must be a multiple of the block size.
    void encrypt(const QByteArray &clearText, QByteArray &cipherText);
    void decrypt(const QByteArray &cipherText, QByteArray &clearText);

    // Serialization of parameters
    bool toStream(QDataStream &s);
    bool fromStream(QDataStream &s);

protected:
    EncryptionAlgorithm algorithm;
    QString keyFile;
    Encryptor *encryptor;

private:
    Q_DISABLE_COPY(Cipher)
};

#endif // CIPHER_H
